<?php
/***
 * 碎片
 */
namespace app\api\controller;

use app\api\controller\BaseController;

class DebrisController extends BaseController{

	/**
	*
    *列表
	**/
	public function getList(){
		$param		= input('post.');
        $lang			= (input('post.lang')) ? input('post.lang') : 'en';	// 语言类型
        $token		= input('post.token/s');
        $userArr	= explode(',',auth_code($token,'DECODE'));
        $uid		= $userArr[0];
        $username		= $userArr[1];

		$where = 'a.status !=-1 and a.uid = '.$uid;
//		if(isset($param['uid']) && !empty($param['uid'])){
//			$where.=" and a.uid = {$param['uid']}";
//		}
		if(isset($param['status']) && !empty($param['status'])){
			$where.=" and a.status = {$param['status']}";
		}
		if(isset($param['keywords']) && !empty($param['keywords'])){
			$where.=" and (n.name like '%{$param['keywords']}%' or u.nickname like '%{$param['keywords']}%' )";
		}

		// if($token){
		// 	$userArr	= explode(',',auth_code($token,'DECODE'));
		// 	$uid		= $userArr[0];
		// 	$where.=" and a.uid=$uid";

		// }
		//每页记录数
		$pageSize	= (isset($param['pagesize']) and $param['pagesize']) ? $param['pagesize'] : 10;
		//当前页
		$page		= (isset($param['page']) and $param['page']) ? $param['page'] : 1;
		//p($where);
		$fields="a.*,n.name,n.file,n.credit_type,u.username,u.nickname,u.header";
        $data	= model('Debris')->alias('a')
            ->field($fields)
			->join('nft n','a.nft_id=n.id','left')
			->join('users u','n.uid=u.id','left')
            ->where($where)
            ->order('a.create_time desc')
            ->paginate($pageSize)
            ->toArray();

        $statusArr=model('Debris')->getStatus();
		foreach ($data['data'] as $key => &$value) {
			$value['file']=$value['file']?($_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$value['file']):'';
            $value['header']=$value['header']?($_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$value['header']):'';
            $value['create_time']=date('Y-m-d H:i:s',$value['create_time']);
//            $value['statusStr']=$statusArr[$value['status']];
            $value['credit_type_str']= getCreditTypeName($value['credit_type']);
		}
		return json(['code'=>1,'data'=>$data]);
	}

    public function getRoy(){
        $token			= (input('post.token')) ? input('post.token') : '';
        $userArr	= explode(',',auth_code($token,'DECODE'));
        $uid		= $userArr[0];

        $roy = db('users_roy')
            ->join('users u','ly_users_roy.uid=u.id','left')
            ->where(array("u.id"=>$uid,"status"=>1))->find();
        $royData = array();
        //合并版税到碎片列表:
        if ($roy){
            $royTask = db('task')->where("status","-1")->find();
            $royData = array(
                "name"=>$royTask['title'],
                "available_number"=>$roy['number'],
                "file"=>'/img/goods_roy.png',
                "header"=>'',
                'create_time'=>date('Y-m-d H:i:s',$roy['add_time']),
                'credit_type'=>'USDT',
                "priceUp"=>0,
                "reward_price"=>1,
                "pledge_number"=>0,
            );
        }
        return json(['code'=>1,'data'=>$royData]);
    }

    /**
	*
    *详情
	**/
	public function getDetail(){
        $lang			= (input('post.lang')) ? input('post.lang') : 'en';	// 语言类型
		$id		= input('post.id');
        if(empty($id)){
            return json(['code'=>0,'code_dec'=>config($lang.'.lang2')[0]]);
        }
        $token			= (input('post.token')) ? input('post.token') : '';
		$where = "a.status !=-1  and  a.id=$id ";
		if($token){
			$userArr	= explode(',',auth_code($token,'DECODE'));
			$uid		= $userArr[0];
			$where.=" and a.uid=$uid";

		}
		$fields="a.*,n.name,n.file,n.credit_type,u.username,u.nickname,u.header";
        $data	= db('users_goods')->alias('a')
            ->field($fields)
			->join('nft n','a.nft_id=n.id','left')
			->join('users u','n.uid=u.id','left')
            ->where($where)
            ->find();
        if(empty($data)){
            return ['code'=>0,'code_dec'=>config($lang.'.lang2')[1]];
        }
		//p($data);
        // $statusArr=model('Nft')->getStatus();
		$data['file']=$data['file']?($_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$data['file']):'';
        $data['create_time']=date('Y-m-d H:i:s',$data['create_time']);
        //$data['statusStr']=$statusArr[$data['status']];
        $data['credit_type_str']= getCreditTypeName($data['credit_type']);

		return json(['code'=>1,'data'=>$data]);
	}



}
